<?php
App::uses('AppController', 'Controller');
/**
 * Backend Controller
 *
 * @property Backend $Backend
 */
class BackendController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->loadModel('Comment');
		$contacts = $this->Comment->find('all', array('conditions' => array('Comment.status' => Comment::active,
																														'Comment.type' => Comment::contact)));
		$this->set('contacts', $contacts);

		$comments = $this->Comment->find('all', array('conditions' => array('Comment.status' => Comment::active,
																														'Comment.type' => Comment::comment)));
		$this->set('comments', $comments);
	}

/**
 *
 * updateCommentStatus
 */
	public function admin_notAllowComment(){
		if(!$this->request->is('ajax')){
			throw new BadRequestException(__('Invalid Request'));
		}
		$this->loadModel('Comment');
		$comment = $this->Comment->findById($_POST['cid']);
		if($comment){
			$this->Comment->read(null, $_POST['cid']);
			$this->Comment->set(array('status' => Comment::deactive));
			$this->Comment->save();
		}
		//get active comments
		$comments = $this->Comment->find('all', array('conditions' => array('Comment.status' => Comment::active,
																														'Comment.type' => Comment::comment)));
		$this->set('comments', $comments);
		$this->render('/Elements/Comments/_comments', 'ajax');
	}

/**
 * Dev: Hung Nguyen
 * replyContact
 */
	public function admin_replyContact(){
		if(!$this->request->is('ajax')){
			throw new BadRequestException(__('Invalid Request'));
		}
		$this->loadModel('Comment');
		if(isset($this->request->data['email'])){
			App::uses('CakeEmail', 'Network/Email');
			$email = new CakeEmail('smtp');
			#$email->template('ask_question');
			$email->emailFormat('html');
			#$email->viewVars(array('comment' => $comment));
			$email->to($this->request->data['email']['to']);
			$email->subject($this->request->data['email']['subject']);
			$email->message($this->request->data['email']['body']);
			if($email->send()){
				
			}else{
				
			}
		}
		$comment = $this->Comment->findById($_POST['cid']);
		if($comment){
			$this->set('contact', $comment);						
		}
		$this->render('_replyForm', 'ajax');
	}
}
